package com.nf.health.app.datamain.activity;

import android.os.Bundle;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.adapter.BloodSugarHistoryDataAdapter;
import com.nf.health.app.adapter.OnMyClickListener;
import com.nf.health.app.customview.DragListView;
import com.nf.health.app.http.RequestProvider;
import com.nf.health.app.models.BloodSugarDataBean;
import com.nf.health.app.utils.ViewUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by hepann on 2016/3/31.
 * The only genius that is worth anything is the genius for hard work
 *
 * @author hepann
 * @Description 血糖数据历史activity
 * @email he_pann@163.com
 */
public class BloodSugarHistoryDataActivity extends BaseActivity {

    private String pageNo = "1";
    private String pageSize = "15";
    private int totalPageNo = 1; //刷新时记录请求的总pageNo数

    protected RequestProvider provider;
    private static final String REQUEST_BLOOD_SUGAR_HISTORY_DATA = "request_blood_sugar_history_data"; //首次请求数据flag
    private static final String REQUEST_MORE_BLOOD_SUGAR_DATA = "request_more_blood_sugar_data"; //请求更多数据flag
    private static final String REQUEST_DELETE_DATA = "request_delete_data"; //删除数据

    //血糖历史记录bean类
    private BloodSugarDataBean bean;
    private BloodSugarHistoryDataAdapter adapter;

    @Bind(R.id.history_listview)
    DragListView historyListview;
    private int deletePosition = -1;//记录删除位置,删除后直接移除更新ui

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.layout_common_header);
        setContent(R.layout.activity_blood_sugar_history_data);
        ButterKnife.bind(this);

        if (provider == null) {
            provider = new RequestProvider(this);
        }

        initView();
        //请求历史数据
        provider.requestBloodSugarDataNew(pageNo, pageSize, REQUEST_BLOOD_SUGAR_HISTORY_DATA);
    }

    private void initView() {
        ViewUtil.setText(this, R.id.titlebar_title_tv, "历史记录");
        historyListview.setRefreshableAndLoadMoreable(true, false);
        historyListview.setOnRefreshAndLoadMoreListener(new DragListView.onRefreshAndLoadMoreListener() {
            @Override
            public void onRefresh() {
                totalPageNo++;
                provider.requestBloodSugarDataNew(totalPageNo + "", pageSize, REQUEST_MORE_BLOOD_SUGAR_DATA);
            }

            @Override
            public void onLoadMore() {

            }
        });
    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        if (REQUEST_BLOOD_SUGAR_HISTORY_DATA.equals(actionName)) {
            //首次请求数据成功
            bean = (BloodSugarDataBean) object;
            if (adapter == null) {
                adapter = new BloodSugarHistoryDataAdapter(this, bean);
                adapter.setOnMyClickListener(new OnMyClickListener() {
                    @Override
                    public void onClick(int position, String flag) {
                        deletePosition = position;
//                        showToast("删除id=" + bean.data.deviceData.get(position).id);
                        provider.delHistoryData(bean.data.deviceData.get(position).id + "", "bloodsugar", REQUEST_DELETE_DATA);
                    }
                });
            }
            historyListview.setAdapter(adapter);
        } else if (REQUEST_MORE_BLOOD_SUGAR_DATA.equals(actionName)) {
            //请求更多数据成功
            historyListview.completeRefresh();
            BloodSugarDataBean moreBeans = (BloodSugarDataBean) object;
            if (moreBeans.data.deviceData.size() == 0) {
                showToast("没有更多数据了");
            } else {
                bean.data.deviceData.addAll(moreBeans.data.deviceData);
                adapter.setNewBeanAndNotify(bean);
            }
        } else if (REQUEST_DELETE_DATA.equals(actionName)) {
            showToast("删除成功");
            if (deletePosition != -1) {
                bean.data.deviceData.remove(deletePosition);
                adapter.setNewBeanAndNotify(bean);
            }
        }
    }
}
